package com.longrise.android.byjk.plugins.tabthird;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.MineMultipleitem;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/3/9.
 * 我的页面的主rcv的adapter
 */

public class MineRcvAdapter extends BaseMultiItemQuickAdapter<MineMultipleitem, BaseViewHolder> {

    List<EntityBean> mBeanList = new ArrayList<>();
    private OnMineItemClickListener mOnMineItemClickListener;

    public MineRcvAdapter(List<MineMultipleitem> data) {
        super(data);
        addItemType(MineMultipleitem.TYPE1, R.layout.minefragment_item1);
        addItemType(MineMultipleitem.TYPE2, R.layout.minefragment_item2);
    }

    public void setData(EntityBean[] beans, List<MineMultipleitem> list) {
        mBeanList.clear();
        for (EntityBean bean : beans) {
            mBeanList.add(bean);
        }
        setNewData(list);
    }

    @Override
    protected void convert(BaseViewHolder helper, MineMultipleitem item) {
        switch (helper.getItemViewType()) {
            case MineMultipleitem.TYPE1:  //横排是三个item的样式
                parseType1(helper);
                break;
            case MineMultipleitem.TYPE2:  //常用工具类型
                parseType2(helper);
                break;
        }

    }

    /**
     * 解析样式一 横排是三个item的样式
     */
    private void parseType1(BaseViewHolder helper) {
        int layoutPosition = helper.getLayoutPosition() - 1;
        View convertView = helper.getConvertView();
//        if (layoutPosition == 0) {
//            helper.setVisible(R.id.minefragment_item1_devider, false);
//        } else {
//            helper.setVisible(R.id.minefragment_item1_devider, true);
//        }
        EntityBean entityBean = mBeanList.get(layoutPosition);
        EntityBean[] childmenuses = entityBean.getBeans("childmenus");
        if (childmenuses == null) {
            return;
        }
        for (int x = 0; x < childmenuses.length; x++) {
            EntityBean bean = childmenuses[x];
            final String showname = bean.getString("showname");
            String picpath = bean.getString("picpath");
            String hasmessage = bean.getString("hasmessage");
            final String uniqueid = bean.getString("uniqueid");
            final String linkedtype = bean.getString("linkedtype");
            final String linkedurl = bean.getString("linkedurl");
            final String fullurl = bean.getString("fullurl");
            String areaname = entityBean.getString("areaname");
            String backgroundurl = entityBean.getString("backgroundurl");
            helper.setText(R.id.minefragment_item1_title_tv1, areaname);
            ImageView titleiv = (ImageView) convertView.findViewById(R.id.minefragment_item1_title_iv1);
            Glide.with(AppUtil.getContext()).load(backgroundurl).into(titleiv);
            switch (x) {
                case 0:
                    ImageView iv = (ImageView) convertView.findViewById(R.id.minefragment_item1_iv1);
                    helper.setText(R.id.minefragment_item1_tv1, showname);
                    Glide.with(AppUtil.getContext()).load(picpath).into(iv);
                    if ("0".equals(hasmessage)) {
                        helper.setVisible(R.id.minefragment_item1_point1, false);
                    } else {
                        helper.setVisible(R.id.minefragment_item1_point1, true);
                    }
                    LinearLayout ll1 = (LinearLayout) convertView.findViewById(R.id.minefragment_item1_ll1);
                    ll1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnMineItemClickListener != null) {
                                mOnMineItemClickListener.onClick(uniqueid, linkedtype, showname, linkedurl, fullurl, null);
                            }
                        }
                    });
                    break;
                case 1:
                    ImageView iv1 = (ImageView) convertView.findViewById(R.id.minefragment_item1_iv2);
                    helper.setText(R.id.minefragment_item1_tv2, showname);
                    Glide.with(AppUtil.getContext()).load(picpath).into(iv1);
                    if ("0".equals(hasmessage)) {
                        helper.setVisible(R.id.minefragment_item1_point2, false);
                    } else {
                        helper.setVisible(R.id.minefragment_item1_point2, true);
                    }
                    LinearLayout ll2 = (LinearLayout) convertView.findViewById(R.id.minefragment_item1_ll2);
                    ll2.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnMineItemClickListener != null) {
                                mOnMineItemClickListener.onClick(uniqueid, linkedtype, showname, linkedurl, fullurl, null);
                            }
                        }
                    });
                    break;
                case 2:
                    ImageView iv2 = (ImageView) convertView.findViewById(R.id.minefragment_item1_iv3);
                    helper.setText(R.id.minefragment_item1_tv3, showname);
                    Glide.with(AppUtil.getContext()).load(picpath).into(iv2);
                    if ("0".equals(hasmessage)) {
                        helper.setVisible(R.id.minefragment_item1_point3, false);
                    } else {
                        helper.setVisible(R.id.minefragment_item1_point3, true);
                    }
                    LinearLayout ll3 = (LinearLayout) convertView.findViewById(R.id.minefragment_item1_ll3);
                    ll3.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mOnMineItemClickListener != null) {
                                mOnMineItemClickListener.onClick(uniqueid, linkedtype, showname, linkedurl, fullurl, null);
                            }
                        }
                    });
                    break;
            }
        }

    }

    /**
     * 解析样式二 //常用工具类型
     */
    private void parseType2(BaseViewHolder helper) {
        int layoutPosition = helper.getLayoutPosition() - 1;
        View convertView = helper.getConvertView();
        RelativeLayout mRlmore = (RelativeLayout) convertView.findViewById(R.id.minefragment_item2_more_rl);
        if (layoutPosition == 0) {
            helper.setVisible(R.id.minefragment_item2_devider, false);
        } else {
            helper.setVisible(R.id.minefragment_item2_devider, true);
        }
        EntityBean bean = mBeanList.get(layoutPosition);
        helper.setText(R.id.minefragment_item2_title, bean.getString("areaname"));
        ImageView titleIv = (ImageView) convertView.findViewById(R.id.minefragment_item2_iv1);
        Glide.with(AppUtil.getContext()).load(bean.getString("backgroundurl")).into(titleIv);
        final String loadmoretype = bean.getString("loadmoretype");
        if ("1002".equals(loadmoretype)) {  //是否显示 加载更多
            mRlmore.setVisibility(View.VISIBLE);
            mRlmore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnMineItemClickListener.onClick(null, null, null,
                            null, null, loadmoretype);
                }
            });
        } else {
            mRlmore.setVisibility(View.GONE);
            mRlmore.setOnClickListener(null);
        }


        RecyclerView rcv = (RecyclerView) convertView.findViewById(R.id.minefragment_item2_rcv);
        MineRcvitem2Adapter adapter = new MineRcvitem2Adapter();
        rcv.setLayoutManager(new GridLayoutManager(mContext, 4));
        rcv.setAdapter(adapter);
        EntityBean[] childmenuses = bean.getBeans("childmenus");

        ViewGroup.LayoutParams layoutParams = rcv.getLayoutParams();
        int itemHeight = AppUtil.dip2px(93);
        if (childmenuses.length <= 4) {
            layoutParams.height = itemHeight;
        } else {
            layoutParams.height = itemHeight * 2;
        }
        rcv.setLayoutParams(layoutParams);
        adapter.setDatas(childmenuses);

        adapter.setOnMineItem2ClickListener(new MineRcvitem2Adapter.OnMineItem2ClickListener() {
            @Override
            public void onClick(String uniqueid, String linkedtype, String showname, String linkedurl, String fullurl) {
                if (mOnMineItemClickListener != null) {
                    mOnMineItemClickListener.onClick(uniqueid, linkedtype, showname, linkedurl, fullurl, null);
                }
            }
        });
    }

    public void setOnMineItemClickListener(OnMineItemClickListener listener) {
        this.mOnMineItemClickListener = listener;
    }

    public interface OnMineItemClickListener {
        void onClick(String uniqueid, String linkedtype, String showname, String linkedurl,
                     String fullurl, String loadmoretype);
    }
}
